<template>
  <!--新增药品  唐美莉 -->
  <section>
    <p class="retail_title">零售药品</p>
    <table class="table-box">
      <thead>
        <tr>
          <th>药品名称</th>
          <th>规格</th>
          <th>生产厂商</th>
          <th>零售价</th>
          <th>拆零价</th>
          <th>批号</th>
          <th>有效期</th>
          <th>库存数量</th>
          <th>数量</th>
          <th>单位</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item,index) in itemList" :key="index">
          <td v-if="item.show" colspan="11">
            <el-select
              class="durg_option_box"
              @change="changeDurg(item)"
              v-model="item.durg"
              clearable
              remote
              reserve-keyword
              filterable
              :remote-method="remoteMethod"
              placeholder="请选择"
            >
              <el-option value :key="-1" label>
                <p class="durg_title_box">
                  <span>药品名称</span>
                  <span>规格</span>
                  <span>生产厂家</span>
                  <span>零售价</span>
                  <span>批号</span>
                  <span>有效期</span>
                  <span>库存数量</span>
                </p>
              </el-option>
              <el-option v-for="it in tableList" :key="it.id" :value="it" :label="it.generalName">
                <p class="durg_title_box">
                  <span>{{it.goodsName}}</span>
                  <span>{{it.specs}}</span>
                  <span>{{it.factory}}</span>
                  <span>￥{{it.retailPrice}}</span>
                  <span>{{it.lotNumber}}</span>
                  <span>{{it.lotNumber}}</span>
                  <span>{{it | residueDurg(dwList) }}</span>
                </p>
              </el-option>
            </el-select>
          </td>
          <td v-if="!item.show">{{item.sourceName}}</td>
          <td v-if="!item.show">{{item.specs}}</td>
          <td v-if="!item.show">{{item.supplierName}}</td>
          <td v-if="!item.show">￥{{item.retailPrice}}</td>
          <td v-if="!item.show">{{item.pieceMoney?'￥'+item.pieceMoney:'/'}}</td>
          <td v-if="!item.show">
            <el-select v-model="item.lotNumber">
              <el-option
                v-for="(its,index) in item.lotNumberList"
                :key="index"
                :value="its"
                :label="its"
              ></el-option>
            </el-select>
          </td>
          <td :class="{'over':item.isOverdue}" v-if="!item.show">
            {{item.effectiveTime | changeTime}}
            <label v-if="item.isOverdue">已过期</label>
          </td>
          <td v-if="!item.show">{{item | residueDurg(dwList)}}</td>
          <td v-if="!item.show">
            <el-input v-model="item.count"></el-input>
          </td>
          <td v-if="!item.show">
            <unit v-model="item.minUnit" :unit="item.minUnit" :list="item.unitList"></unit>
            <!-- <el-select v-model="item.minUnit">
              <el-option v-for="(un,index) in item.unitList" :key="index" :value="un" :label="un"></el-option>
            </el-select>-->
          </td>
          <td v-if="!item.show">
            <el-link :underline="false" @click="removeItem(index)" type="danger">删除</el-link>
          </td>
        </tr>
      </tbody>
    </table>
    <el-row>
      <el-col :span="24">
        <el-button @click="addItem()" type="text">
          <i class="el-icon-plus"></i>
          添加
        </el-button>
      </el-col>
    </el-row>
    <br />
    <el-row>
      <el-col :span="24">
        <el-button type @click="onSubmit">收费</el-button>
      </el-col>
    </el-row>
    <pay v-if="dialogPay" v-model="dialogPay" :item="itemList" :totalMoney="totalMoney"></pay>
    <!-- <pager v-model="pager" @change-pagenum="getData"></pager> -->
  </section>
</template>
<script>
export default {
  name: "retailAdd",
  data() {
    return {
      dialogPay: false,
      curItem: null,
      pager: { pageSize: 10, pageNum: 1, total: 0 },
      tableList: [],
      itemList: [
        {
          retailId: "", //零售药品Id
          sourceName: "", //药品名
          drugId: "", //药品id
          batchId: "", //批次
          showNum: "", //剩余数量
          specs: "", //规格
          lotNumber: "", //批号
          retailNum: 1, //零售数量
          retailPrice: "", //零售价
          effectiveTime: "", //有效期
          unit: "", //拆零单位
          packUnit: "", //正常单位
          minUnit: "", //变动单位
          sellMoney: "", //零售
          pieceMoney: "", //拆零价格
          isPiece: 0, //是否可拆零
          show: true,
          //拆分
          psc: "",
          pscUnit: "",
          ass: "",
          assunit: "",
          supplierName: "", //生产厂商
          isOverdue: false,
          unitList: [], //自定义单位list
          lotNumberList: "", //自定义批号
          durg: "" //自定义药品信息
        }
      ],
      dwList: [],
      totalMoney: 0,
      newTime: "",
      qxBtns: {},
      dataList: [],
      distype: "2",
      setType: "1"
    };
  },
  created() {
    let th = this;
    th.getQX();
    th.getData();
    th.getBasic();
    //获取当前日期
    th.newTime = th.getNewDate();
  },
  methods: {
    remoteMethod(query) {
      let th = this;
      setTimeout(() => {
        th.$api
          .pageStockBatchV2({
            pageSize: 20,
            pageNum: 1,
            vagueKey: query,
            source: 1,
            type: 1
          })
          .then(res => {
            if (res.executed) {
              if (res.dataBody != undefined) {
                th.tableList = res.dataBody.list;
                // th.pager.total = res.dataBody.total;
              } else {
                th.tableList = [];
                th.pager.total = 0;
              }
            }
            th.showLoading(false);
          });
      }, 200);
    },
    getData(type) {
      let th = this;
      if (type == -1) {
        th.pager.pageNum = 1;
        th.pager.total = 0;
      }
      let j = { type: 1, source: 1 };
      j = Object.assign(j, th.pager);
      th.showLoading();
      th.$api.pageStockBatchV2(j).then(res => {
        if (res.executed) {
          if (res.dataBody != undefined) {
            th.tableList = res.dataBody.list;
            // th.pager.total = res.dataBody.total;
          } else {
            th.tableList = [];
            th.pager.total = 0;
          }
        }
        th.showLoading(false);
      });
    },
    changeDurg(item) {
      let th = this;
      item.show = false;
      item = Object.assign(item, item.durg);
      item.sourceName = item.durg.goodsName;
      item.drugId = item.durg.sourceId;
      item.effectiveTime = item.durg.effectiveTime;
      if (
        new Date(item.effectiveTime).getTime() > new Date(th.newTime).getTime()
      ) {
        item.isOverdue = false;
      } else {
        item.isOverdue = true;
      }
      item.batchId = item.durg.id;
      let unit = [];
      if (item.durg.residueNumUnit != item.durg.residueNumSubsectionUnit) {
        if (item.durg.pieceMoney == undefined) {
          unit.push(item.durg.residueNumUnit);
        } else {
          unit.push(item.durg.residueNumUnit);
          unit.push(item.durg.residueNumSubsectionUnit);
        }
      } else {
        unit.push(item.durg.residueNumUnit);
      }
      item.unitList = unit;
      item.minUnit = item.durg.residueNumUnit;
      th.$api
        .pageStockBatchV2({
          pageNum: 1,
          pageSize: 1000,
          vagueKey: item.sourceName,
          type: 1,
          source: 1
        })
        .then(res => {
          if (res.executed) {
            if (res.dataBody != undefined) {
              let list = res.dataBody.list;
              let ls = [];
              for (let i = 0; i < list.length; i++) {
                ls.push(list[i].lotNumber);
              }
              item.lotNumberList = ls;
            } else {
              item.lotNumberList = [];
            }
          }
          th.showLoading(false);
        });
    },
    onSubmit() {
      let th = this;
      let item = th.itemList;
      let isOver = false;
      let total = 0;
      for (let i = 0; i < item.length; i++) {
        if (item[i].count == "" || item[i].count == 0) {
          th.$message.error("请输入药品数量!");
          return false;
        } else {
          if (item[i].pieceMoney != undefined) {
            if (item[i].minUnit == item[i].residueNumUnit) {
              total += item[i].retailPrice * item[i].count;
            } else {
              total += item[i].pieceMoney * item[i].count;
            }
          } else {
            if (item[i].count > item[i].residueNum) {
              th.$message.error("剩余库存不足！");
              return false;
            } else {
              if (
                item[i].retailPrice != undefined &&
                item[i].retailPrice != ""
              ) {
                total += item[i].retailPrice * item[i].count;
              }
            }
          }
        }
      }
      th.totalMoney = total;
      for (let i = 0; i < item.length; i++) {
        if (item[i].isOverdue == true) {
          isOver = true;
          break;
        }
      }
      if (isOver == true) {
        th.$confirm(`药品已过期，是否继续操作？?`, "系统提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true
        }).then(() => {
          th.dialogPay = true;
        });
      } else {
        th.dialogPay = true;
      }
    },
    getBasic() {
      let th = this;
      th.getCommonConfigByKey("药品剂量单位").then(res => {
        th.dwList = res;
      });
    },
    addItem() {
      let th = this;
      let json = {
        retailId: "", //零售药品Id
        sourceName: "", //药品名
        drugId: "", //药品id
        batchId: "", //批次
        showNum: "", //剩余数量
        specs: "", //规格
        lotNumber: "", //批号
        retailNum: 1, //零售数量
        retailPrice: "", //零售价
        effectiveTime: "", //有效期
        unit: "", //拆零单位
        packUnit: "", //正常单位
        minUnit: "", //变动单位
        show: true,
        isOverdue: false,
        unitList: [], //自定义单位list
        lotNumberList: "", //自定义批号
        durg: "" //自定义药品信息
      };
      th.remoteMethod("");
      th.itemList.push(json);
    },
    removeItem(index) {
      this.itemList.splice(index, 1);
    }
  },
  components: {
    pay: () => import("./pay"),
    unit: () => import("@/pages/_com/_com/unitSelect")
  }
};
</script>
<style lang="scss" scoped>
.retail_title {
  font-size: 18px;
  line-height: 40px;
  border-bottom: 1px solid #ddd;
}
.over {
  color: #ef1717;
}
.durg_option_box {
  width: 100%;
}
.durg_title_box {
  display: inline-block;
  width: 100%;
  > span {
    width: 15%;
    display: inline-block;
  }
}
</style>